package com.base.components.database.service;

import com.base.components.common.service.database.DatabaseExecutor;
import com.base.components.database.dao.base.JpaHelper;
import org.springframework.lang.NonNull;

import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * DatabaseExecutor
 *
 * @author <a href="drakelee1221@gmail.com">LiGeng</a>
 * @version v1.0.0
 * @date 2018-07-27 9:52
 */
public class JpaDatabaseExecutor implements DatabaseExecutor {

  @Override
  @SuppressWarnings("unchecked")
  public <ID> List<ID> queryIds(@NonNull String sql){
    return JpaHelper.getEntityManager().createNativeQuery(sql).getResultList();
  }

  @Override
  @SuppressWarnings("unchecked")
  public List<Map<String, Object>> queryRows(@NonNull String sql, int page, int pageSize){
    Query query = JpaHelper.getEntityManager().createNativeQuery(sql).setFirstResult((page - 1) * pageSize).setMaxResults(pageSize);
    JpaHelper.setMapResult(query);
    return query.getResultList();
  }
}
